package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.InformationDistributionListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class InformationDistributionListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();
    public interface InformationDistributionListItemClickListener{
        void onItemClick(int position);
    }
    private InformationDistributionListItemClickListener listener;

    public InformationDistributionListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            InformationDistributionListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.information_distribution_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        InformationDistributionListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onItemClick(position);
            }
        });
        JSONObject item = items.getJSONObject(position);
        String type = StringUtil.getValue(item.getString("type"));
        JSONObject flow = item.getJSONObject("flow");
        root.title.setText(StringUtil.getValue(flow.getString("Title")));
        root.typename.setText(StringUtil.getValue(item.getString("typename")));
        String sendertime = StringUtil.getValue(item.getString("sendertime"));
        if(sendertime.contains("T"))
            sendertime = sendertime.substring(0, sendertime.indexOf("T"));
        root.sendertime.setText(sendertime);
        root.status.setText(StringUtil.getValue(item.getString("status")));
        if("1".equals(type))
            root.typename.setBackgroundResource(R.color.item_tv_bg_i);
        else if("2".equals(type))
            root.typename.setBackgroundResource(R.color.item_tv_bg_ii);
        else if("3".equals(type))
            root.typename.setBackgroundResource(R.color.item_tv_bg_iv);
        else if("4".equals(type))
            root.typename.setBackgroundResource(R.color.item_tv_bg_iii);
        else if("5".equals(type))
            root.typename.setBackgroundResource(R.color.item_tv_bg_iv);

        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(InformationDistributionListItemClickListener listener){
        this.listener = listener;
    }

    private class ViewHolder {
        private InformationDistributionListItemBinding binding;

        public ViewHolder(InformationDistributionListItemBinding binding) {
            this.binding = binding;
        }

        public InformationDistributionListItemBinding getBinding() {
            return binding;
        }
    }
}
